//
//  ZKBaseWebViewController.swift
//  ZKNASProj
//
//  Created by gaoguangxiao on 2022/12/5.
//

import UIKit
import WebKit
import SVProgressHUD

class ZKBaseWebViewController: ZKBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        automaticallyAdjustsScrollViewInsets = false
        view.addSubview(webView)
        webView.snp.makeConstraints { make in
            make.top.equalTo(ZKAppInfo.topSafeHeight)
            make.left.bottom.right.equalTo(0)
        }

    }
    
    /// load HTMLFile
    open func loadHTMLFile(with string: String,_ type: String) {
        if string.count == 0 { return }
        SVProgressHUD.show()
        let url = Bundle.main.url(forResource: string, withExtension: type)!
        webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
    }
    
    lazy var webView: WKWebView = {
        let wkUController = WKUserContentController();
        let configuration = WKWebViewConfiguration()
        if #available(iOS 10.0, *) {
            configuration.mediaTypesRequiringUserActionForPlayback = []
        }
        
        
        let jScript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        let wkUScript = WKUserScript(source: jScript, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: true)
        wkUController.addUserScript(wkUScript)
        configuration.userContentController = wkUController
        // 创建设置对象
        let preference = WKPreferences()
        preference.minimumFontSize = 15
        configuration.preferences = preference
        
        let tmpWebView = WKWebView(frame: .zero, configuration: configuration)
        tmpWebView.backgroundColor = .white
        tmpWebView.clipsToBounds = true
        tmpWebView.uiDelegate = self
        tmpWebView.navigationDelegate = self
        //tmpWebView.isMultipleTouchEnabled = true
        tmpWebView.scrollView.alwaysBounceVertical = true
        //tmpWebView.scrollView.showsVerticalScrollIndicator = false
        //tmpWebView.scrollView.showsHorizontalScrollIndicator = false
        //tmpWebView.customUserAgent = BBUserAgentManager.shared.customUserAgent?["User-Agent"]
        let observation = tmpWebView.observe(\.title) { [weak self] (wkWebView, _) in
            guard wkWebView.title?.count ?? 0 > 1 else {
                return
            }
        }
        if #available(iOS 11.0, *) {
            tmpWebView.scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
        //webViewCreated = true
        tmpWebView.backgroundColor = .clear
        tmpWebView.scrollView.backgroundColor = .clear
        tmpWebView.isOpaque = false
        return tmpWebView
    }()

}

extension ZKBaseWebViewController : WKUIDelegate ,WKNavigationDelegate {
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        SVProgressHUD.dismiss()
    }
    
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!,
                        withError error: Error) {
        SVProgressHUD.dismiss()
    }
}
